package com.yunfeng.supercall.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.ly.quickdev.library.activity.DevBaseListActivity;
import com.ly.quickdev.library.adapter.ListBaseAdapter;
import com.ly.quickdev.library.adapter.ListBaseAdapter.ViewHolder;
import com.ly.quickdev.library.utils.JsonUtils;
import com.ly.quickdev.library.view.SquareImageView;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.FriendsShareBean;
import com.yunfeng.supercall.bean.UserBean;
import com.yunfeng.supercall.httputils.HttpClient;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

/**
 * 好友分享列表
 * 
 * @author 洋
 * 
 */
public class FriendShareListActivity extends DevBaseListActivity<FriendsShareBean>
		implements Constants {

	private HttpClient mHttpClient = new HttpClient();
	private View mHeaderView;
	private int mOtherId;

	@Override
	public void setContentView(int layoutResID) {
		// TODO Auto-generated method stub
		super.setContentView(R.layout.activity_friends_share_list);
	}

	@Override
	public void onSupportContentChanged() {
		// TODO Auto-generated method stub
		super.onSupportContentChanged();
		mHeaderView = getLayoutInflater().inflate(
				R.layout.list_header_share_list, null);
		mListView.getRefreshableView().addHeaderView(mHeaderView, null, false);
		mListView.getRefreshableView().setDescendantFocusability(
				ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		UserBean mUser = BaseActivity.getUser(getActivity());
		mUser.setupUserView(mHeaderView);
	}

	/**
	 * 点赞
	 * 
	 * @param mFriendsShare
	 */
	private void zanThis(final FriendsShareBean mFriendsShare) {
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put("i_shareid", mFriendsShare.getShareid());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(Constants.FUNCTION_ADD_SHARE_ZAN, json,
				new MyAjaxCallBack() {

					@Override
					public void onReceiveError(String msg, String errorCode) {
						// TODO Auto-generated method stub
						showToast(msg);
					}

					@Override
					public void onReceiveData(String data, String msg) {
						// TODO Auto-generated method stub
						showToast(R.string.zan_success);
						int i = Integer.valueOf(mFriendsShare.getZannum());
						i++;
						mFriendsShare.setZannum(i + "");
						mAdapter.notifyDataSetChanged();
					}
				});
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_friends_share, parent, false);
		}
		final ImageView avatar = ViewHolder.get(convertView, R.id.avatar);
		final TextView content = ViewHolder.get(convertView, R.id.content);
		final TextView name = ViewHolder.get(convertView, R.id.user_name);
		final FriendsShareBean friendsShare = mList.get(position);
		final TextView time = ViewHolder.get(convertView, R.id.date);
		final TextView commentCount = ViewHolder.get(convertView,
				R.id.comment_count);
		ViewHolder.get(convertView, R.id.zan_area).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						zanThis(friendsShare);
						// startActivity(new
						// Intent(FriendShareListActivity.this,
						// FriendsShareDetailActivity.class).putExtra(
						// KEY_DATA, friendsShare));
						;
					}
				});
		ViewHolder.get(convertView, R.id.comment_area).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						startActivity(new Intent(FriendShareListActivity.this,
								FriendsShareDetailActivity.class).putExtra(
								KEY_DATA, friendsShare));
						;
					}
				});
//		final TextView zanCount = ViewHolder.get(convertView, R.id.zan_count);
		commentCount.setText(friendsShare.getPinglunnum());
		content.setText(friendsShare.getContent());
//		zanCount.setText(friendsShare.getZannum());
		AppContext.displayImgWithDefault(avatar, friendsShare.getPhoto());
		name.setText(friendsShare.getNick_name());
		time.setText(friendsShare.getCreatetime());
		GridView gridView = ViewHolder.get(convertView, R.id.gridview);
		gridView.setAdapter(new GridImgAdapter(getActivity(), friendsShare
				.getPhotoList()));
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				ActivityOptionsCompat optionsCompat = ActivityOptionsCompat
						.makeScaleUpAnimation(view, (int) view.getWidth() / 2,
								(int) view.getHeight() / 2, 0, 0);
				// ActivityOptionsCompat optionsCompat = ActivityOptionsCompat
				// .makeThumbnailScaleUpAnimation(view,
				// ((BitmapDrawable) ((ImageView) view)
				// .getDrawable()).getBitmap(), 0, 0);
				startActivity(
						new Intent(FriendShareListActivity.this,
								ViewPhotoActivity.class).putExtra(KEY_INDEX,
								position)
								.putStringArrayListExtra(
										KEY_DATA,
										(ArrayList<String>) friendsShare
												.getPhotoList()), optionsCompat
								.toBundle());
			}
		});
		content.setFocusable(false);
		content.setFocusableInTouchMode(false);
		gridView.setFocusable(false);
		gridView.setFocusableInTouchMode(false);
		gridView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		return convertView;
	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub
		mOtherId = getIntent().getIntExtra(KEY_DATA, 0);
		TextView title = findView(R.id.title);
		if (mOtherId == BaseActivity.getUser(getActivity()).getUserid()) {
			title.setText(R.string.my_share);
		} else if (mOtherId == -3) {
			title.setText(R.string.enterprise_share);
		} else if (mOtherId == -2) {
			title.setText(R.string.focused_share);
		} else if (mOtherId == -1) {
			title.setText(R.string.fans_share);
		}
		JSONObject json = BaseActivity.getBaseParams(getActivity());
		try {
			json.put(KEY_OTHER_ID, mOtherId);
			json.put(KEY_PAGE_NUM, pageNo);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(FUNCTION_GET_SHARE_LIST, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub
				showList();
				mListView.onRefreshComplete();
			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				if (mList == null) {
					mList = new ArrayList<FriendsShareBean>();
				}
				if (pageNo == 0) {
					mList.clear();

				}
				mList.addAll(JsonUtils.parseList(data, FriendsShareBean.class));
				setListAdapter();
				mListView.onRefreshComplete();
				setEmptyMessage(getString(R.string.no_share));
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	public static class GridImgAdapter extends ListBaseAdapter<String> {

		public GridImgAdapter(Context context, List<String> list) {
			super(context, list);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View initView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (convertView == null) {
				SquareImageView imageView = new SquareImageView(getContext());
				imageView.setScaleType(ScaleType.CENTER_CROP);
				convertView = imageView;
			}

			AppContext.displayImage(convertView, getItem(position));
			return convertView;
		}

	}

}
